/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen;

import java.io.PrintStream;
import java.util.List;
import java.util.Collections;

/** Call a method.
 * <p>
 * A {@code Call} expression is made up of an optional object subexpression and an argument list.
 * If an object subexpression is not provided, the method call leaves the object implicit, as in {@code bar()}, as opposed to {@code foo.bar()}.
 * The argument list can be provided as a {@code List<Expr>} or a {@link String} of Java code.
 * The object subexpression can also be provided as a Java expression {@link String} if the argument list is provided as a {@link String}.
 * A {@code Call} is evaluated by first evaluating the object subexpression, if there is one, then evaluating the arguments in order, then finally performing the method call.
 * The result of a {@code Call} is the value that the method call returns.
 * A {@code Call} expression can be optionally assigned an id.
 * </p>
 * <p>
 * You can call a static method by using a {@link NoFX} expression with the class name as the "object" subexpression, or by using a {@link String} of the class name as the object subexpression.
 * This works even though a class name by itself is technically not an expression.
 * </p>
 * <p>
 * It is an error to try to use the result of a {@code Call} if the return type of the method you call is {@code void}.
 * Assigning an id, or wrapping the {@code Call} in a {@link Proxy} counts as trying to use the result.
 * </p>
 */
public final class Call extends TupleExpr
{
	private final int line;
	private final String type;
	private final Expr obj;
	private final String meth;
	private final String argstr;

	/** Construct a {@code Call} with a {@code List<Expr>} for arguments.
	 * @param c The {@link Config} that will contain this expression.
	 * @param l The line number this expression was on.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param o An optional object subexpression.
	 * @param m The method to call.
	 * @param e A list of subexpressions to use as method call arguments. Can be empty, but not null.
	 */
	public Call(Config c,int l,String t,String i,Expr o,String m,List<Expr> e)
	{
		super(c,i,e);
		line = l;
		type = t;
		obj = o;
		meth = m;
		argstr = null;
	}

	/** Construct a {@code Call} with a Java argument list {@link String}.
	 * @param c The {@link Config} that will contain this expression.
	 * @param l The line number this expression was on.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param o An optional object subexpression.
	 * @param m The method to call.
	 * @param a A Java argument list, not including parentheses, to use as method call arguments. Use an empty string for no arguments. E.g.: {@code "someID, true"}, {@code ""}
	 */
	public Call(Config c,int l,String t,String i,Expr o,String m,String a)
	{
		super(c,i,Collections.<Expr>emptyList());
		line = l;
		type = t;
		obj = o;
		meth = m;
		argstr = a;
	}

	/** Construct a {@code Call} with a Java object subexpression {@link String} and a Java argument list {@link String}.
	 * @param c The {@link Config} that will contain this expression.
	 * @param l The line number this expression was on.
	 * @param t The result type of this expression, or null.
	 * @param i The id to assign to this expression, or null.
	 * @param o An object subexpression provided as a Java expression {@link String}. Cannot be null.
	 * @param m The method to call.
	 * @param a A Java argument list, not including parentheses, to use as method call arguments. Use an empty string for no arguments. E.g.: {@code "someID, true"}, {@code ""}
	 */
	public Call(Config c,int l,String t,String i,String o,String m,String a) {this(c,l,t,i,new NoFX(l,o),m,a);}

	void printType(PrintStream out) throws InvalidConfigException
	{
		if (type == null) throw new InvalidConfigException(line,"Need return type for method call: " + meth + '(' + ((argstr == null) ? "..." : argstr) + ')');
		out.print(type);
	}

	void statBefore() {if (obj != null) obj.statize();}
	boolean hasStats() {return super.hasStats() || (obj != null && obj.hasStats());}

	void printStatsDlgt(PrintStream out,int tabs) throws InvalidConfigException
	{
		if (obj != null) obj.printStats(out,tabs);
		super.printStatsDlgt(out,tabs);
	}

	void printCore(PrintStream out)
	{
		if (obj != null)
		{
			obj.printExpr(out);
			out.print('.');
		}
		out.print(meth);
		out.print('(');
		if (argstr == null) printElems(out);
		else out.print(argstr);
		out.print(')');
	}
}
